package book

import (
	"comic/internal/pb"
	"common"
	"common/grpcx"
	"common/zapx"
	"context"
	"go.uber.org/zap"
	"time"
)

// IsFavourite 是否收藏了漫画
func IsFavourite(grpcCtx context.Context, comicId int32) int32 {
	ctx, cancel := common.WithMetaDataOutGoingTimeoutContext(time.Second*2, grpcx.GetUserMetaDataByGrpc(grpcCtx))
	defer cancel()
	conn, err := grpcx.NewNoCertConn(
		"book",
	)
	if err != nil {
		zapx.Logger.Error("连接到book服务失败", zap.Error(err))
		return 0
	}
	defer conn.Close()
	call := pb.NewBookClient(conn)
	res, err := call.IsFavourite(ctx, &pb.IsFavouriteRequest{
		ComicId: comicId,
	})
	if err != nil {
		return 0
	}
	return res.IsFavourite
}
